/*
 * @Date: 2024-11-11 14:28:03
 * @Author: LiSong
 * @LastEditors: LiSong
 * @LastEditTime: 2024-12-18 11:18:48
 * @Description: 专家维护 
 */
import XEUtils from 'xe-utils';
import HandlerUtils from '@/handler/utils/HandlerUtils.js'

import { reactive } from "vue"
import { defAxios as request } from '@/utils/http'

import { callComp } from '@hd/base/service/HdCallCompService.js'
import { RkConfirmBox } from '@/components/message-box/index.js'

import ExpertInfoDialog from '@/handler/dialogs/ExpertInfoDialog.vue'
import ExpertRuleDialog from '@/handler/dialogs/ExpertRuleDialog.vue'

const useLayoutHandler = (params) => {
  // 参数
  const { routeQuery, userInfo, upTableSearch, downTableSearch } = params

  // 基本信息
  const baseData = reactive({
    // 选中项目ID
    proId: '',
    // 选中区划编码
    adCode: userInfo.adBjCode,
    // 单位ID
    agId: '',
    // 选中单位编码
    agCode: '',
    // 单位名称
    agName: '',
    // 上级单位Id
    agParentId: '',
    // 工作流
    processKey: routeQuery.processKey,
  })

  /**
   * 表单渲染业务按钮点击事件
   */
  const buttonClick = async (params) => {
    console.log('buttonClick', params)
    const btnType = params.type
    if (btnType === 'add') {
      const addInfo = {
        ...XEUtils.clone(baseData, true),
        formData: {
          adName: userInfo.adCode,
          AD_CODE: userInfo.adCode
        }
      }
      handleOpenExpertDialog('add', addInfo)
    }
    else if (btnType === 'edit') {
      const { row } = params
      const reqParams = {
        formCode: routeQuery.tbFormCode,
        expertId: row.expertId,
      }
      const result = await request({ url: `/expert/get`, method: 'post', data: reqParams })
      if (result.code === 200) {
        handleOpenExpertDialog('edit', {
          ...XEUtils.clone(baseData, true),
          formData: result.data
        })
      } else {
        HandlerUtils.failedMsgTip("获取专家详细信息错误！", true)
      }
    }
    else if (btnType === 'rule') {
      callComp.open(ExpertRuleDialog, {
        extParams: { data: params.row },
        onConfirm: async (data) => {
          upTableSearch()
        }
      })
    }
    else if (btnType === 'delete') {
      RkConfirmBox({
        title: '提示',
        content: `您确定要${params.name}吗？`,
        onSubmit: async function () {
          const expertIds = params.rows.map(item => {return item.expertId}).join(",")
          const result = await request({ url: `/expert/delete`, method: 'post', data: { expertIds } })
          if (result.code === 200) {
            HandlerUtils.successMsgTip(params.name)
          } else {
            HandlerUtils.failedMsgTip(result.msg, true)
          }
        }
      })
    }
  }

  /**
   * 打开专家信息弹窗
   * 
   * @param {String} type 类型
   * @param {Object} info 信息
   */
  const handleOpenExpertDialog = (type, info) => {
    callComp.open(ExpertInfoDialog, {
      type: type,
      title: '专家信息注册',
      formCode: routeQuery.tbFormCode,
      compType: 'expert',
      className: 'rk-modal zjwh-dialog',
      extParams: {
        ...info,
        formType: 'expert',
      },
      onConfirm: async (data) => {
        HandlerUtils.successMsgTip('保存')
      }
    })
  }

  return {
    baseData,
    buttonClick,
    btnGroupRightClick: null
  }
}

export default useLayoutHandler